package tk.yangzheng.ballscore

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class MainViewModel : ViewModel() {
    private val _scoreOfA: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>().also {
            it.value = 0
        }
    }

    private val _scoreOfB: MutableLiveData<Int> by lazy {
        MutableLiveData<Int>().also {
            it.value = 0
        }
    }

    val scoreOfA: LiveData<Int> = _scoreOfA
    val scoreOfB: LiveData<Int> = _scoreOfB

    private var lastScoreOfA: Int = 0
    private var lastScoreOfB: Int = 0

    // A队加分
    fun plusScoreOfTeamA(n: Int) {
        lastScoreOfA = _scoreOfA.value!!
        lastScoreOfB = _scoreOfB.value!!

        _scoreOfA.value = _scoreOfA.value?.plus(n)
    }

    // B队加分
    fun plusScoreOfTeamB(n: Int) {
        lastScoreOfA = _scoreOfA.value!!
        lastScoreOfB = _scoreOfB.value!!

        _scoreOfB.value = _scoreOfB.value?.plus(n)
    }

    fun undo() {
        _scoreOfA.value = lastScoreOfA
        _scoreOfB.value = lastScoreOfB
    }

    fun reset() {
        lastScoreOfA = _scoreOfA.value!!
        lastScoreOfB = _scoreOfB.value!!

        _scoreOfA.value = 0
        _scoreOfB.value = 0
    }
}